package io.geekidea.boot.system.controller;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.system.dto.ResourceTypeDto;
import io.geekidea.boot.system.query.ResourceTypeQuery;
import io.geekidea.boot.system.service.ResourceTypeService;
import io.geekidea.boot.system.vo.ResourceTypeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.geekidea.boot.framework.annotation.Log;

import javax.validation.Valid;
import java.util.List;

/**
 * 资源类型 控制器
 */
@RestController
@Tag(name = "资源类型")
@RequestMapping("/admin/resourceType")
public class ResourceTypeController {

    private static final Logger logger = LoggerFactory.getLogger(ResourceTypeController.class);

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 添加资源类型
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(value = "添加资源类型", type = SysLogType.ADD)
    @PostMapping("/addResourceType")
    @Operation(summary = "添加资源类型")
    @Permission("sys:resourcetype:add")
    public ApiResult<Boolean> addResourceType(@RequestBody ResourceTypeDto dto) {

        boolean result = resourceTypeService.addResourceType(dto);

        return ApiResult.success(result);
    }

    @PostMapping("/updateResourceType")
    @Operation(summary = "修改资源类型")
    @Permission("sys:resourcetype:update")
    public ApiResult<Boolean> updateResourceType(@RequestBody ResourceTypeDto dto) {
        logger.info("Request to update resource type: {}", dto);
        boolean result = resourceTypeService.updateResourceType(dto);
        logger.info("Resource type updated: {}", result);
        return ApiResult.success(result);
    }

    @PostMapping("/deleteResourceType/{id}")
    @Operation(summary = "删除资源类型")
    @Permission("sys:resourcetype:delete")
    public ApiResult<Boolean> deleteResourceType(@PathVariable Long id) {
        logger.info("Request to delete resource type with id: {}", id);
        boolean result = resourceTypeService.deleteResourceType(id);
        logger.info("Resource type deleted: {}", result);
        return ApiResult.success(result);
    }

    @PostMapping("/getResourceType/{id}")
    @Operation(summary = "获取资源类型详情")
    public ApiResult<ResourceTypeVo> getResourceTypeById(@PathVariable Long id) {
        logger.info("Request to get resource type by id: {}", id);
        ResourceTypeVo resourceType = resourceTypeService.getResourceTypeById(id);
        logger.info("Resource type retrieved: {}", resourceType);
        return ApiResult.success(resourceType);
    }

    @PostMapping("/getResourceTypeList")
    @Operation(summary = "获取资源类型列表")
    public ApiResult<List<ResourceTypeVo>> getResourceTypeList(@Valid @RequestBody ResourceTypeQuery query) {
        logger.info("Request to get resource type list with query: {}", query);
        List<ResourceTypeVo> list = resourceTypeService.getResourceTypeList(query);
        logger.info("Resource type list retrieved: {}", list);
        return ApiResult.success(list);
    }
}
